﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using System.Collections.Generic;
using Baosight.ZNH.ZMCL.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 待整改隐患
    /// </summary>
    public partial class ZMCL73 : EFViewCmes
    {
        public ZMCL73()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 页签切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            EFTab _tab = sender as EFTab;
            if (null != _tab)
            {
                EFTabItem _item = _tab.SelectedItem as EFTabItem;

                if (null != _item && this.ModifyedRisk.IsSelected)
                {
                    this.ModifyedRisk.LoadView<EFTabItem>("ZMCL74");
                }
            }
        }

        /// <summary>
        /// 整改反馈
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnRectifyButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup("整改反馈", eiInfo, "隐患整改-反馈", "insert", null, sender, e);
        }

        /// <summary>
        /// 隐患整改反馈
        /// 弹出页面
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            RiskModifying _modifyBack = new RiskModifying(_dialogType);
            EIRow _eIRow = _eIInfo.GetBlock("result").GetRow(0);
            
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.empName"));

            _modifyBack.EIDataContext = new Platform.Core.UI.EIDataContext();
            _modifyBack.EIDataContext.Update(_eIInfo);

            //_modifyBack.fdKeyId.Value = _eIRow["fdKeyId"].ToString();//隐患处理ID
            //_modifyBack.fdRiskRecordId.Value = _eIRow["fdRiskRecordId"].ToString();//隐患记录ID
            _modifyBack.fdSendnoteDate.Value = _eIRow["fdDeadlineDate"].ToString();//解决期限

            _modifyBack.fdFeedbackPeopleId.Value = this.EIDataContext.GetAttribute("fdFeedbackPeopleId");
            _modifyBack.fdFeedbackPeople.Value = this.EIDataContext.GetAttribute("fdFeedbackPeople");

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _modifyBack;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(PopupWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 新增窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PopupWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            //按钮生效
            base.TempButtonOpen(true);

            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.Grid.Refresh();
            }
        }

        /// <summary>
        /// 记录条选择切换时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (null != this.Grid.SelectedItem)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
                string fdIsRepeat = _row["fdIsRepeat"] == null ? "" : _row["fdIsRepeat"].ToString();

                //是否重复隐患
                if (!fdIsRepeat.Equals("否"))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdRiskRecordId", _row["fdRiskRecordId"].ToString());
                    this.riskDetails.LoadView<EFCustomViewContainerPanel>("ZMCL71", args);//加载从表页面

                    Dictionary<string, string> args1 = new Dictionary<string, string>();
                    args1.Add("fdPenaltyIds", _row["fdPenaltyIds"] == null ? "" : _row["fdPenaltyIds"].ToString());
                    this.riskPenalty.LoadView<EFCustomViewContainerPanel>("ZMCL75", args1);//加载从表页面
                }
                else
                {
                    this.riskDetails.Content = null;
                    this.riskPenalty.Content = null;
                }
            }
            else
            {
                this.riskDetails.Content = null;
                this.riskPenalty.Content = null;
            }
        }

    }
}
